<?php

class Sb_Form extends Zend_Form
{
	
	protected $params;
	
	public function __construct(array $params = null,$options = null )
	{
		$this->params = $params;
		$this->setParam( 'formId',get_class( $this ) )
				->setParam( 'formErrorClass','ui-state-error' )
				->setParam( 'elmentsClass','ui-widget-content ui-corner-all' )
				->setAttrib('id',$this->getParam( 'formId' ))
				->setAttrib('action',$params[ 'action' ])
			 	->setAttrib('class','form ui-form-default');
		$this->setDisableLoadDefaultDecorators( true );
		parent::__construct( $options );
		$this->addDecorator('FormElements')
				->addDecorator('Fieldset',array(
					'class' => 'ui-widget ui-widget-content ui-corner-all',
					'legend'	=> $params[ 'legend' ]
				))
     		 	->addDecorator('Form'); 	 	
	}
	
	public function getParam( $paramName )
	{
		return isset( $this->params[ $paramName ] ) ?  $this->params[ $paramName ]  : null;
	}
	
	public function setParam( $paramName,$paramVal )
	{
		$this->params[ $paramName ]  = $paramVal;
		return $this;
	}
	
	public function getParams(  )
	{
		return $this->params;
	}
	
	public function setParams(  )
	{
		$this->params = $params;
		return $this->params;
	}
	
	public function init( )
    {
    	$elementClass = $this->getParam( 'elmentsClass' );
    	foreach ($this->getElements() as $elm){
    		$type = $elm->getType( );
    		$type = explode('_', $type);
    		$type = strtolower( $type[count($type) - 1 ] );
    		$this->setCustomDecorators( $elm->setAttrib('class',$type.' '.$elementClass )->getName( ) );
    	}
    	$name = $this->getParam( 'formId' ).'Submit';
    	$label = ($text = $this->getParam( 'textSubmit' )) ? $text : 'Submit';
		$this->addElement('submit', $name,
			 array( 
			 	'label'    => $label,
			 	'class' => 'submit ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only'
		) );
        $this->getElement($name)
        		 ->setDecorators(array(
        		 	array('ViewHelper'),
             		array('HtmlTag', array(
             					'tag' => 'div'
             				)
             		)
        	  ));
    }
    
 	protected function setCustomDecorators( $elementName )
    {
    	$decorators = array(
    			array('ViewHelper'), 
    			array('Errors',array('class' => $this->getParam( 'formErrorClass' ))), 
				array('Label',
					array('requiredSuffix' => ' * '
				)),
				array('HtmlTag',
					array('tag' => 'div'))
        );
    	 $this->getElement( $elementName )
        	 	 ->setDecorators(  $decorators );
        return $this;
    }
    
 	public function isSubmit( )
    {
    	$name = $this->getParam( 'formId' ).'Submit';
    	return (bool)Zend_Controller_Front::getInstance()->getRequest()->getParam( $name );
    }
}

